package cn.itcast.web.controller.cargo;



import cn.itcast.domain.cargo.ExportExample;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/cargo/packing")
public class PackingController extends BaseController {

    @Reference
    private PackingService packingService;
    @Reference
    private ExportService exportService;



    //分页显示
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "5")int size){
        PackingExample example = new PackingExample();
        PackingExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        PageInfo info = packingService.findAll(example, page, size);
        request.setAttribute("page", info);
        return "/cargo/packing/packing-list";
    }

    //跳转添加页面
    @RequestMapping("/toPacking")
    public String toPacking(String id){
        request.setAttribute("id",id);
        return "cargo/packing/packing-toPacking";

    }


    //显示报运单列表
    @RequestMapping("/exportList")
    public String packingExportList(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "5")int size){
        ExportExample example = new ExportExample();
        ExportExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        criteria.andStateEqualTo(2L);

        PageInfo pageInfo = exportService.findAll(example,page,size);
        request.setAttribute("page",pageInfo);
        return "cargo/packing/packing-exportList";
    }

    //进入修改页面进行数据回显
    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        Packing packing = packingService.findById(id);
        request.setAttribute("packing",packing);
        return "/cargo/packing/packing-update";
    }


    //添加 或 修改
    @RequestMapping("/edit")
    public String edit(Packing packing){
        packing.setCompanyId(getLoginCompanyId());
        packing.setCompanyName(getLoginCompanyName());
        if (StringUtils.isEmpty(packing.getPackingListId())){
            packingService.save(packing);
        }else {
            packingService.update(packing);
        }
        return "redirect:/cargo/packing/list.do";
    }

    //删除
    @RequestMapping("/deleteById")
    public String deleteById(String id){
        packingService.delete(id);
        return "redirect:/cargo/packing/list.do";
    }





    //装货单提交
    @RequestMapping("/submit")
    public String submit(String id){
        //根据id 查询选货物 修改状态
        Packing packing = new Packing();
        packing.setPackingListId(id);
        packing.setState(1L);
        packingService.update(packing);
        return "redirect:/cargo/packing/list.do";
    }

    //装货单取消
    @RequestMapping("/cancel")
    public String cancel(String id){
        //根据id 查询选货物 修改状态
        Packing packing = new Packing();
        packing.setPackingListId(id);
        packing.setState(0L);
        packingService.update(packing);
        return "redirect:/cargo/packing/list.do";
    }


}
